package com.zhangfb95.crosschain.console.command;

import com.zhangfb95.crosschain.console.enums.CommandEnum;
import com.zhangfb95.crosschain.infra.model.Result;
import com.zhangfb95.crosschain.sdk.CrossChainClient;
import com.zhangfb95.crosschain.sdk.infra.model.api.response.server.rpcchain.RpcGetHeightApiRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhangfubing
 * @since 2024/1/2
 */
@Component
public class ChainInfoCommand implements Command {

    @Autowired
    private CrossChainClient crossChainClient;

    @Override
    public CommandEnum command() {
        return CommandEnum.CHAIN_INFO;
    }

    @Override
    public boolean handle(String[] args) {
        if (args.length < 1 || isHelp(args)) {
            printHelpInfo();
            return false;
        }

        String chainPath = args[0];

        Result<RpcGetHeightApiRes> apiRes = crossChainClient.getHeight(chainPath);
        if (apiRes.isSuccess()) {
            System.out.println("Result: success");
            System.out.println("height: " + apiRes.getData().getHeight());
        } else {
            System.out.println("Result: " + apiRes.getMsg());
        }
        return false;
    }

    private void printHelpInfo() {
        System.out.println("Usage: chainInfo [chainPath]");
    }
}
